/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
 */
package nixonftp.list;

import java.awt.Color;
import java.awt.Rectangle;
import nixonftp.list.NXTableRenderer;
import nixonftp.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.table.TableCellRenderer;

public class NXTable extends JTable implements NXListAdapter {
	private String dir = "";
	public boolean dirty;
	private TableCellRenderer renderer;
	private boolean local;
	private boolean leftSide;
	
	public NXTable(boolean local) {
		super(new Object[][]{
					  {"Mary", "2010 Jan 20", "32 KB"},
					  {"Alison", "1995 Dec 31", "1,023 bytes"},}, new String[]{"Name", "Date", "Size"});
		try {
			setAutoCreateRowSorter(true);
		} catch (NoSuchMethodError ex) {
		}
		setModel(new NXTableModel());
		setDragEnabled(true);
		setRowHeight(20);
		renderer = new NXTableRenderer(true);
		this.local = local;
	}

	public int getSelectedItemCount() {
		return getSelectedRowCount();
	}

	public TableCellRenderer getCellRenderer(int row, int col) {
		return renderer;
	}

	public Rectangle getRealBounds() {
		return ((JScrollPane)this.getParent().getParent()).getBounds();
	}
	
	public NXObjectIndex getSelectedValue() {
		NXTableModel model = (NXTableModel) getModel();
		int row = this.getSelectedRow();
		return (row != -1) ? model.getValueAt(row): null;
	}

	public String getDirectory() {
		return dir;
	}

	public void setDirectory(String s) {
		dir = s;
	}

	public ListModel getAdapterModel() {
			return null;
	}

	public void setModel(ListModel lsm) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public Object[] getSelectedValues() {
		int count = this.getSelectedItemCount();
		NXObjectIndex[] values = new NXObjectIndex[count];
		for (int x = 0; x < count; x++) {
			values[x] = getSelectedValue();
		}
		return values;
	}

	public boolean getType() {
		return true; //table, not list
	}

	public int getIndex() {
		return -1;
	}

	public boolean getLocal() {
		return local;
	}

	public boolean dropLeft() {
		return leftSide;
	}

	public void setDropLeft(boolean b) {
		leftSide = b;
	}

	public void addElement(Object o) {
		NXTableModel model = (NXTableModel) getModel();
		NXObjectIndex obj; NXObjectIndex add = (NXObjectIndex) o;
		for (int x = 0; x < model.getRowCount(); x++) {
			obj = (NXObjectIndex) model.getValueAt(x);

			if (add != null) { //add element, do not add duplicate
				 if (add.name.equals(obj.name)) return;
			}
		}
		model.addRow(add);
		invalidate();
	}

	public void removeElement(String name) {
		 NXTableModel model = (NXTableModel) getModel();
		 NXObjectIndex obj;
		 for (int x = 0; x < model.getRowCount(); x++) {
			  obj = model.getValueAt(x);
			  if (obj.name.equals(name)) {
					model.removeRowAt(x);
					return;
			  }
		 }
	}
}
